package com.test.timmy;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.json.JSONException;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class TeamOverview
	extends Activity
{
	private static final String COMMA = ",";
	private static final String NEWLINE = "\n";

	private static final int IDX_MORALE = 0;
	private static final int IDX_HUMOUR = 1;
	private static final int IDX_SKILL = 2;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.team_overview);
		
		Bundle bundle = getIntent().getExtras();
		if(bundle != null)
		{
			String eventID = bundle.getString(LifeEvent.TAG_EVENT);
			String personID = bundle.getString(Person.TAG_PERSON_ID);
			LifeEvent event = Controller.getInstance().getLifeEvents().get(eventID);
			Person person = Controller.getInstance().getPeople().get(personID);
			try {
				calculateEffectOnPeople(person, event.getChanges());
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		TextView departments = (TextView) findViewById(R.id.departments);
		departments.setText(getDepartmentsOverview());

		TextView people = (TextView) findViewById(R.id.people);
		people.setText(getPeopleOverview());
	}
	
	private String getDepartmentsOverview()
	{
		//0 = morale, 1 = humour, 2 = skill
		Map<String, Integer[]> valuesPerDepartment = new HashMap<String, Integer[]>();
		Iterator<Person> iterator = Controller.getInstance().getPeople().values().iterator();
		StringBuilder sb = new StringBuilder();
		while (iterator.hasNext())
		{
			Person person = iterator.next();
			Integer[] department = valuesPerDepartment.get(person.getDepartment());
			if (department == null)
			{
				department = new Integer[3];
				department[IDX_MORALE] = person.getMorale();
				department[IDX_HUMOUR] = person.getHumour();
				department[IDX_SKILL] = person.getSkill();
				valuesPerDepartment.put(person.getDepartment(), department);
			}
			else
			{
				department[IDX_MORALE] = department[IDX_MORALE] + person.getMorale();
				department[IDX_HUMOUR] = department[IDX_HUMOUR] + person.getHumour();
				department[IDX_SKILL] = department[IDX_SKILL] + person.getSkill();
			}
		}
		
		 Set<Entry<String, Integer[]>> departmentSets = valuesPerDepartment.entrySet();
		 Iterator<Entry<String, Integer[]>> iteratorSets = departmentSets.iterator();
		 while(iteratorSets.hasNext())
		 {
			 Entry<String, Integer[]> department = iteratorSets.next();
			 sb.append(department.getKey() + COMMA + department.getValue()[IDX_MORALE] + COMMA + department.getValue()[IDX_HUMOUR]
					 + COMMA + department.getValue()[IDX_SKILL] + NEWLINE);
		 }
		return sb.toString();
	}
	
	private String getPeopleOverview()
	{
		Iterator<Person> iterator = Controller.getInstance().getPeople().values().iterator();
		StringBuilder sb = new StringBuilder();
		while (iterator.hasNext())
		{
			Person person = iterator.next();
			//morale, humour and skill 
			sb.append(person.getName() + COMMA + person.getMorale() + COMMA
					+ person.getHumour() + COMMA + person.getSkill() + NEWLINE);
		}
		return sb.toString();
	}
	
	private void calculateEffectOnPeople(Person source, Changes changes)
			throws NumberFormatException, JSONException
	{
		Iterator<Person> iterator = Controller.getInstance().getPeople().values().iterator();
		while(iterator.hasNext())
		{
			Person currentPerson = iterator.next();
			int distance = distance(source, currentPerson);
			if (distance < 2)
			{
				//this way there is no need to treat differently the source
				distance = 2;
			}
			currentPerson.setMorale(currentPerson.getMorale() + changes.getMorale() * 2 / distance);
			currentPerson.setHumour(currentPerson.getHumour() + changes.getHumour() * 2 / distance);
			currentPerson.setSkill(currentPerson.getSkill() + changes.getSkill() * 2 / distance);
		}
	}
	
	private int distance(Person source, Person destination)
	{
		int x1 = source.getDesk_x();
		int y1 = source.getDesk_y();
		int x2 = destination.getDesk_x();
		int y2 = destination.getDesk_y();

		double distance = Math.sqrt(Math.pow(x1-x2, 2) + Math.pow(y1-y2, 2));
		return (int)distance;
	}
}
